﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace TheSpirit
{
    public class CardTree
    {
        #region Declaration

        Attribute treeAttribute;
        CardInfo cardInfo;

        Texture2D fireTreeTexture;
        Texture2D waterTreeTexture;
        Texture2D forestTreeTexture;
        Texture2D addTexture;
        Texture2D fireShortCutTexture;
        Texture2D waterShortCutTexture;
        Texture2D forestShortCutTexture;
        Texture2D upgradeShortCutTexture;
        Texture2D fireBallCard;
        Texture2D waterBallCard;
        Texture2D grassCard;
        Texture2D upgradeCard;

        Rectangle treePosition;
        Rectangle addPosition;
        Rectangle fireShortCutRect;
        Rectangle waterShortCutRect;
        Rectangle forestShortCutRect;
        Rectangle upgradeShortCutRect;
        Rectangle basicCardPosition;

        bool selectCard;

        #endregion


        #region Property

        public Attribute.AttributeType TreeAttribute
        {
            get { return treeAttribute.attributeType; }
        }
        public Rectangle TreePosition
        {
            get { return treePosition; }
        }
        public Rectangle AddPosition
        {
            get { return addPosition; }
        }
        public Texture2D FireTreeTexture
        {
            get { return fireTreeTexture; }
        }
        public Texture2D WaterTreeTexture
        {
            get { return waterTreeTexture; }
        }
        public Texture2D ForestTreeTexture
        {
            get { return forestTreeTexture; }
        }

        #endregion


        #region Initialize

        public CardTree()
        {
            treeAttribute = new Attribute();
            treeAttribute.attributeType = Attribute.AttributeType.Fire;

            cardInfo = new CardInfo();

            selectCard = false;
        }

        public void LoadContent(ContentManager content)
        {
            cardInfo.LoadContent(content);

            fireTreeTexture = content.Load<Texture2D>("Strategy/FireTree");
            waterTreeTexture = content.Load<Texture2D>("Strategy/WaterTree");
            forestTreeTexture = content.Load<Texture2D>("Strategy/ForestTree");
            addTexture = content.Load<Texture2D>("Strategy/AddCard");

            fireShortCutTexture = content.Load<Texture2D>("Strategy/FireButton");
            waterShortCutTexture = content.Load<Texture2D>("Strategy/WaterButton");
            forestShortCutTexture = content.Load<Texture2D>("Strategy/ForestButton");
            upgradeShortCutTexture = content.Load<Texture2D>("Strategy/UpButton");

            fireBallCard = content.Load<Texture2D>("Card/Fire/FireBallCard");
            waterBallCard = content.Load<Texture2D>("Card/Water/WaterBallCard");
            grassCard = content.Load<Texture2D>("Card/Forest/GrassCard");
            upgradeCard = content.Load<Texture2D>("Card/UpgradeCard");

            int w = fireTreeTexture.Width;
            int h = fireTreeTexture.Height;
            int x = Constants.WIDTH * 170 / 1280;
            int y = Constants.HEIGHT * 110 / 720;

            treePosition = new Rectangle(x, y, w, h);

            //위치 재설정 필요
            x = Constants.WIDTH * 30 / 1280;
            y = Constants.HEIGHT * 420 / 720;
            w = addTexture.Width;
            h = addTexture.Height;
            
            addPosition = new Rectangle(x, y, w, h);

            w = fireShortCutTexture.Width;
            h = fireShortCutTexture.Height;
            x = Constants.WIDTH * 50 / 1280;
            y = Constants.HEIGHT * 30 / 720;

            fireShortCutRect = new Rectangle(x, y, w, h);

            x += Constants.WIDTH * 210 / 1280;
            waterShortCutRect = new Rectangle(x, y, w, h);

            x += Constants.WIDTH * 210 / 1280;
            forestShortCutRect = new Rectangle(x, y, w, h);

            x += Constants.WIDTH * 210 / 1280;
            upgradeShortCutRect = new Rectangle(x, y, w, h);

            w = fireBallCard.Width;
            h = fireBallCard.Height;
            x = Constants.WIDTH * 130 / 1280;
            y = Constants.HEIGHT * 260 / 720;

            basicCardPosition = new Rectangle(x, y, w, h);
        }

        #endregion


        #region Update & Draw

        public void Update(UseCard useCard, ScreenManager screenManager)
        {
            HandleInput(useCard, screenManager);

            switch(treeAttribute.attributeType)
            {
                case Attribute.AttributeType.Fire:
                    FireUpdate(useCard, screenManager);
                    break;
                case Attribute.AttributeType.Water:
                    WaterUpdate(useCard, screenManager);
                    break;
                case Attribute.AttributeType.Forest:
                    ForestUpdate(useCard, screenManager);
                    break;
                case Attribute.AttributeType.Upgrade:
                    break;
            }
        }

        public void HandleInput(UseCard useCard, ScreenManager screenManager)
        {
            if (InputManager.IsLeftClicked())
            {
                //위치 재설정 필요
                if(InputManager.MousePosition.Intersects(fireShortCutRect))
                {
                    treeAttribute.attributeType = Attribute.AttributeType.Fire;
                }
                if(InputManager.MousePosition.Intersects(waterShortCutRect))
                {
                    treeAttribute.attributeType = Attribute.AttributeType.Water;                
                }
                if(InputManager.MousePosition.Intersects(forestShortCutRect))
                {
                    treeAttribute.attributeType = Attribute.AttributeType.Forest;                
                }
                if (InputManager.MousePosition.Intersects(upgradeShortCutRect))
                {
                    treeAttribute.attributeType = Attribute.AttributeType.Upgrade;
                }

                selectCard = false;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            switch (treeAttribute.attributeType)
            {
                case Attribute.AttributeType.Fire:
                    spriteBatch.Draw(fireTreeTexture, treePosition, Color.White);
                    spriteBatch.Draw(fireBallCard, basicCardPosition, Color.White);
                    break;
                case Attribute.AttributeType.Water:
                    spriteBatch.Draw(waterTreeTexture, treePosition, Color.White);
                    spriteBatch.Draw(waterBallCard, basicCardPosition, Color.White);
                    break;
                case Attribute.AttributeType.Forest:
                    spriteBatch.Draw(forestTreeTexture, treePosition, Color.White);
                    spriteBatch.Draw(grassCard, basicCardPosition, Color.White);
                    break;
                case Attribute.AttributeType.Upgrade:
                    spriteBatch.Draw(upgradeCard, basicCardPosition, Color.White);
                    break;
            }

            spriteBatch.Draw(addTexture, addPosition, Color.White);

            spriteBatch.Draw(fireShortCutTexture, fireShortCutRect, Color.White);
            spriteBatch.Draw(waterShortCutTexture, waterShortCutRect, Color.White);
            spriteBatch.Draw(forestShortCutTexture, forestShortCutRect, Color.White);
            spriteBatch.Draw(upgradeShortCutTexture, upgradeShortCutRect, Color.White);

            if(selectCard)
                cardInfo.Draw(spriteBatch);
        }

        #endregion


        #region Helper
        //카드 추가 버튼
        public void FireUpdate(UseCard useCard, ScreenManager screenManager)
        {
            if (InputManager.MousePosition.Intersects(addPosition))
            {
                if (InputManager.IsLeftClicked())
                {
                    //카드를 덱에 추가
                    useCard.Update(new FireBallCard(), screenManager);
                }
            }

            if (InputManager.MousePosition.Intersects(basicCardPosition))
            {
                if (InputManager.IsLeftClicked())
                {
                    cardInfo.Update("FireBall");
                    selectCard = true;
                }
            }
        }

        public void WaterUpdate(UseCard useCard, ScreenManager screenManager)
        {
            if (InputManager.MousePosition.Intersects(addPosition))
            {
                if (InputManager.IsLeftClicked())
                {
                    //카드를 덱에 추가
                    useCard.Update(new WaterBallCard(), screenManager);
                }
            }
            if (InputManager.MousePosition.Intersects(basicCardPosition))
            {
                if (InputManager.IsLeftClicked())
                {
                    cardInfo.Update("WaterBall");
                    selectCard = true;
                }
            }
        }

        public void ForestUpdate(UseCard useCard, ScreenManager screenManager)
        {
            if (InputManager.MousePosition.Intersects(addPosition))
            {
                if (InputManager.IsLeftClicked())
                {
                    //카드를 덱에 추가
                    useCard.Update(new GrassCard(), screenManager);
                }
            }
            if (InputManager.MousePosition.Intersects(basicCardPosition))
            {
                if (InputManager.IsLeftClicked())
                {
                    cardInfo.Update("Grass");
                    selectCard = true;
                } 
            }
        }

        public void UpgradeUpdate(UseCard useCard, ScreenManager screenManager)
        {
            if (InputManager.MousePosition.Intersects(addPosition))
            {
                if (InputManager.IsLeftClicked())
                {
                    //카드를 덱에 추가
                    useCard.Update(new UpgradeCard(), screenManager);
                }
            }
            if (InputManager.MousePosition.Intersects(basicCardPosition))
            {
                if (InputManager.IsLeftClicked())
                {

                }
            }
        }

        #endregion

    }
}
